﻿Public Class dsBXH
    Dim ctx As New QLDBDataContext
    Dim _clb As New dsCLB
    Dim _tht As New Thang_Hoa_Thua
    Dim _list As New List(Of dsBXH)
    'Dim _loadLai As New List(Of dsBXH)
    'Dim _loadLai1Doi As New dsBXH
    '  Private _xepHang As Integer
    Private _doiBong As String
    Private _soTran As Integer
    Private _thang As Integer
    Private _hoa As Integer
    Private _bai As Integer
    Private _hieuSo As Integer
    'Private _banThua As Integer
    Private _diem As Integer


    Public Sub New()
        '    _xepHang = 0
        _doiBong = ""
        _soTran = 0
        _thang = 0
        _hoa = 0
        _bai = 0
        _hieuSo = 0
        '_banThua = 0
        _diem = 0
    End Sub

    'Public Property XepHang() As Integer
    '    Get
    '        Return _xepHang
    '    End Get
    '    Set(value As Integer)
    '        _xepHang = value
    '    End Set
    'End Property
    Public Property DoiBong() As String
        Get
            Return _doiBong
        End Get
        Set(value As String)
            _doiBong = value
        End Set
    End Property

    Public Property SoTran() As Integer
        Get
            Return _soTran
        End Get
        Set(value As Integer)
            _soTran = value
        End Set
    End Property

    Public Property Thang() As Integer
        Get
            Return _thang
        End Get
        Set(value As Integer)
            _thang = value
        End Set
    End Property

    Public Property Hoa() As Integer
        Get
            Return _hoa
        End Get
        Set(value As Integer)
            _hoa = value
        End Set
    End Property

    Public Property Bai() As Integer
        Get
            Return _bai
        End Get
        Set(value As Integer)
            _bai = value
        End Set
    End Property

    Public Property HieuSo() As Integer
        Get
            Return _hieuSo
        End Get
        Set(value As Integer)
            _hieuSo = value
        End Set
    End Property

  

    Public Property Diem() As Integer
        Get
            Return _diem
        End Get
        Set(value As Integer)
            _diem = value
        End Set
    End Property

    Public Function TinhDiem_ThangThua(ByVal vong As Integer, ByVal ma As Integer) As Integer
        Dim hieuSo = HieuSoTheoVong(vong, ma)
        If hieuSo > 0 Then
            Return 3
        ElseIf hieuSo = 0 Then
            Return 1
        Else
            Return 0
        End If
    End Function


    Public Function HieuSoTheoVong(ByVal vong As Integer, ByVal ma As Integer) As Integer
        'Dim query = (From p In ctx.TRANDAUs
        '                 Where p.Vong = vong And p.MaCLB1 = ma Or p.MaCLB2 = ma
        '                 Select p.SoBanThang_CLB1 - p.SoBanThang_CLB2).First()
        'If DoiNha_DoiKhach(ma, vong) = True Then
        '    Return query
        'Else
        '    Return 0 - query
        'End If

        If DoiNha_DoiKhach(ma, vong) Then
            Dim query = (From p In ctx.TRANDAUs
                         Where p.Vong = vong And p.MaCLB1 = ma
                         Select p.SoBanThang_CLB1 - p.SoBanThang_CLB2).First()
            Return query
        Else
            Dim query = (From p In ctx.TRANDAUs
                       Where p.Vong = vong And p.MaCLB2 = ma
                       Select p.SoBanThang_CLB2 - p.SoBanThang_CLB1).First()
            Return query
        End If
    End Function
    Public Function SapXep(ByVal ds As List(Of dsBXH)) As List(Of dsBXH)
        Dim tam As dsBXH
        For i = 0 To 19
            For j = 0 To 19
                If ds(i).Diem > ds(j).Diem Then
                    tam = ds(i)
                    ds(i) = ds(j)
                    ds(j) = tam
                End If
                If ds(i).Diem = ds(j).Diem And ds(i).HieuSo > ds(j).HieuSo Then
                    tam = ds(i)
                    ds(i) = ds(j)
                    ds(j) = tam
                End If
            Next

        Next
        Return ds
    End Function
    Public Function DoiNha_DoiKhach(ByVal maDoi As Integer, ByVal vong As Integer) As Boolean
        Dim query = (From p In ctx.TRANDAUs
                    Where p.Vong = vong And p.MaCLB1 = maDoi).Count()
        If query > 0 Then
            Return True
        Else
            Return False
        End If

    End Function
    Public Function THT_KQ(ByVal vong As Integer, ByVal madoi As Integer) As Thang_Hoa_Thua
        Dim tht As New Thang_Hoa_Thua
        If HieuSoTheoVong(vong, madoi) < 0 Then
            tht.Thang = 0
            tht.Hoa = 0
            tht.Bai = 1
        ElseIf HieuSoTheoVong(vong, madoi) = 0 Then
            tht.Thang = 0
            tht.Hoa = 1
            tht.Bai = 0
        Else
            tht.Thang = 1
            tht.Hoa = 0
            tht.Bai = 0
        End If
        Return tht


    End Function

    Public Function TaoDongDuLieu(ByVal vong As Integer, ByVal ma As Integer) As dsBXH
        Dim dong As New dsBXH
        dong.DoiBong = _clb.LayTenDoiBong(ma)
        dong.SoTran = vong
        dong.Thang = dong.THT_KQ(vong, ma).Thang
        dong.Hoa = dong.THT_KQ(vong, ma).Hoa
        dong.Bai = dong.THT_KQ(vong, ma).Bai
        dong.HieuSo = HieuSoTheoVong(vong, ma)
        dong.Diem = TinhDiem_ThangThua(vong, ma)
        Return dong
    End Function
    Public Function NapDongDuLieuVaoList(ByVal vong As Integer) As List(Of dsBXH)


        For i = 1 To 20
            Dim _dong As New dsBXH
            _dong = TaoDongDuLieu(vong, i)
            _list.Add(_dong)
        Next
        Return _list
    End Function
    Public Sub CapNhatBXH1DB(ByVal ma As Integer)
        Dim query As dsBXH = (From p In ctx.BANGXHs
                    Where p.MaCLB = ma
                    Select New dsBXH With {.DoiBong = _clb.LayTenDoiBong(p.MaCLB), .SoTran = p.SoTran,
                                           .Thang = p.Thang, .Bai = p.Thua, .HieuSo = p.HieuSo, .Diem = p.Diem}).First()
        Dim u As dsBXH = TaoDongDuLieu(query.SoTran + 1, ma)
        query.SoTran = u.SoTran
        query.Thang += u.Thang
        query.Hoa += u.Hoa
        query.Bai += u.Bai
        query.HieuSo += u.HieuSo
        query.Diem += u.Diem

        ctx.SubmitChanges()
    End Sub
    'Public Function CapNhatBXHToanBo(ByVal ma As Integer) As List(Of dsBXH)



    'End Function


    Public Function LoadBXH() As List(Of dsBXH)
        Dim query = From p In ctx.BANGXHs
                    Select New dsBXH With {.DoiBong = _clb.LayTenDoiBong(p.MaCLB), .SoTran = p.SoTran,
                                           .Thang = p.Thang, .Bai = p.Thua, .HieuSo = p.HieuSo, .Diem = p.Diem}

        Return query.ToList()
    End Function
   

    'Public Function CongDongDuLieu(ByVal ma As Integer) As List(Of dsBXH)
    '    Dim _dong As New dsBXH
    '    _dong = TaoDongDuLieu(1, ma)
    '    Dim query = From p In ctx.BANGXHs
    '                Select p.MaCLB = _dong.DoiBong
    'End Function
  

End Class
